<?php

namespace app\index\validate;

use think\Validate;
use think\File as ThinkFile;

class Avatar extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'	=>	['规则1','规则2'...]
     *
     * @var array
     */
	protected $rule = [
        'file' => 'file|fileMime:image/jpeg,image/png|minWidthAndHeight:200,200',
    ];

    protected $message = [
        'file.file' => '头像必须是上传文件',
        'file.fileMime' => '头像必须是jpeg或png格式的图片',
        'file.minWidthAndHeight' => '头像的清晰度不够，宽和高需要208px以上',
    ];

    /**
     * 验证上传头像最小宽高
     * @Author   zhanghong(Laifuzi)
     * @DateTime 2019-02-19
     * @param    string             $value 字段值
     * @param    string             $rule  字段值验证值
     * @return   string/true               验证结果
     */
    public function minWidthAndHeight($file, $rule)
    {
        if (!($file instanceof ThinkFile)) {
            return false;
        }
        if(empty($rule)){
            return false;
        }
        $rule = explode(',', $rule);
        $min_width = intval($rule[0]);
        if(isset($rule[1])){
            $min_height = intval($rule[1]);
        }else{
            $min_height = 0;
        }

        list($width, $height) = getimagesize($file->getRealPath());
        if($width < $min_width || $height < $min_height){
            return false;
        }

        return true;
    }
}
